﻿using System;
using System.Collections.Generic;
using System.Text;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.ArcMapUI;

namespace MyEngineExercise
{
    public class ExampleMethods
    {
        /// <summary>
        /// How to add a layer to a map from a layer file 
        /// </summary>
        /// <param name="pMap"></param>
        /// <param name="path"></param>
        public static void AddLayerFileToMap(IMap pMap, string path)
        {
            IMapDocument pMapDocument = new MapDocumentClass();
            //Use the map document class to open the layer.
            pMapDocument.Open(path, "");
            //When opening a layer with the map document class,
            //the layer is added to a new document as the first
            //layer in the first map.
            pMap.AddLayer(pMapDocument.get_Map(0).get_Layer(0));
        }

        /// <summary>
        /// How to access maps and layers via the MapDocument 
        /// </summary>
        /// <param name="path"></param>
        public static void OpenMXDViaMapDocument(string path)
        {
            IMapDocument pMapDocument = new MapDocumentClass();
            if (pMapDocument.get_IsMapDocument(path))
            {
                pMapDocument.Open(path, null);
                IMap pMap;
                for (int i = 0; i <= pMapDocument.MapCount - 1; i++)
                {
                    pMap = pMapDocument.get_Map(i);
                    Console.WriteLine("pMap.Name:" + pMap.Name);
                    IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
                    pEnumLayer.Reset();
                    ILayer pLayer = pEnumLayer.Next();
                    while (null != pLayer)
                    {
                        Console.WriteLine("pLayer.Name:"+pLayer.Name);
                        pLayer = pEnumLayer.Next();
                    }
                }
            }
        }

        /// <summary>
        /// How to add different types of layers to a map 
        /// </summary>
        public static void AddShapeFile(IMap map)
        {
            // Create a new ShapefileWorkspaceFactory object and
            // open a shapefile folder - the path works with standard 9.3 installation
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspqce = (IFeatureWorkspace)workspaceFactory.OpenFromFile(
                "e:\\Program Files\\ArcGIS\\DeveloperKit\\SamplesNET\\data\\Y2000HurricaneData", 0);
            // Create a new FeatureLayer and assign a shapefile to it
            IFeatureLayer featureLayer = new FeatureLayerClass();
            featureLayer.FeatureClass = featureWorkspqce.OpenFeatureClass("2000_hrcn");
            ILayer layer = (ILayer)featureLayer;
            layer.Name = featureLayer.FeatureClass.AliasName;
            // Add the Layer to the focus map
            //IMxDocument mxDocument=(IMxDocument)(m_application.Document);
            //IMap map = mxDocument.FocusMap;
            map.AddLayer(layer);
        }
    }
}
